package com.hqyj.controller;

import com.hqyj.pojo.Admin;
import com.hqyj.pojo.Manager;
import com.hqyj.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/getAll")
    public String getAll(ModelMap modelMap) {
        List<Manager> all = adminService.getAll();
        System.out.println(all);
        modelMap.put("managerList", all);
        return "showManagerList";
    }


    @RequestMapping("/addManager")
    @ResponseBody
    public Map<String, String> addManager(Manager manager) {
        int i = adminService.addManager(manager);
        Map<String, String> map = new HashMap<>();
        if (i != 0) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "no");

        }
        return map;
    }


    @RequestMapping("/updateManager")
    @ResponseBody
    public Map<String, String> updateManager(Manager manager) {
        int i = adminService.updateManager(manager);
        Map<String, String> map = new HashMap<>();
        if (i != 0) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "no");

        }
        return map;
    }

    @RequestMapping("/delManager")
    @ResponseBody
    public Map<String, String> delManager(Manager manager) {
        int i = adminService.delManager(manager);
        Map<String, String> map = new HashMap<>();
        if (i != 0) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "no");

        }
        return map;
    }


//    @RequestMapping("/adminLogin")
//    @ResponseBody
//    public String adminLogin(String username,String password){
//        Admin admin =adminService.adminLogin(username, password);
//        if(admin != null){
//            return "1";
//        }else {
//            return "0" ;
//        }
//    }

    /*
去到系统管理员登录成功的主页面
*/
    @RequestMapping("/success2")
    public String success2(){
        return "index" ;
    }

    /*
去到宿舍管理员登录成功的主页面
*/
    @RequestMapping("/success")
    public String success(){
        return "index2" ;
    }

    /*
   去到注册的页面
    */
    @RequestMapping("/register")
    public String register(){
        return "register" ;
    }

    /*
     宿舍管理员点击消息去到的页面
     */
    @RequestMapping("/massage")
    public String massage(){return "massage" ;}

    /*
    系统管理员点击消息去到的页面
    */
    @RequestMapping("/massage2")
    public String massage2(){return "massage2" ;}




    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(String username, String password){
        //检查请求参数

        //调用服务层实现业务
        System.out.println("开始调用login");

        return adminService.login(username, password);

    }

    @RequestMapping("/logout")
//    @ResponseBody
    public String logout(){
        //调用服务层实现业务
      adminService.logout();
//        ModelAndView mv = new ModelAndView("redirect:login");
//        return mv;
        return "login";
    }

    @RequestMapping("/welcome")
    public String welcome(){
        return "welcome" ;
    }
}
